package com.tma.intern;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;

/**
 * Servlet implementation class ServletExp
 */
public class UploadFileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public static final String PATH = "PDFfile/";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadFileServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		FileItem item = null;
		try{
				FileUpload fup=new FileUpload();
				boolean isMultipart = FileUpload.isMultipartContent(request);
				// Create a new file upload handler
				System.out.println(isMultipart);
				DiskFileUpload upload = new DiskFileUpload();
	
				// Parse the request
				List /* FileItem */ items = upload.parseRequest(request);
	
				Iterator iter = items.iterator();
				
				while (iter.hasNext()) {
	
					item = (FileItem) iter.next();
		
					if (item.isFormField()) {
						System.out.println( "its a field ");
					} else {
						System.out.println( "its a file" );
						System.out.println(item.getName());
						byte[] in = item.get();
						ConvertImage toImage = new ConvertImage(in);
						int a = item.getName().lastIndexOf("/");
						int b = item.getName().lastIndexOf("\\");
						int start = a > b ? a : b;
						start = start > 0 ? start : 0;
						String name = item.getName().substring(start,item.getName().lastIndexOf("."));
						String path = getServletContext().getRealPath("/") +PATH+name;
						File f = new File(path);
						if(!f.exists()){
						    f.mkdirs();
						}
						int size = toImage.pageNumber();
						for (int i = 0 ; i < size ; i++){							
							FileOutputStream  fstream = new FileOutputStream (path+"/"+i+".png");
							byte[] byteBuff = toImage.toImage(i+1);
							fstream.write(byteBuff);
							fstream.close();
						}
						toImage.closeFile();
					}
				}

			}catch(Exception e){
				e.printStackTrace();
				File cfile=new File(item.getName());
				File tosave=new File(getServletContext().getRealPath("/") +PATH+ cfile.getName());
				try {
					item.write(tosave);
				} catch (Exception exx)
				{
					e.printStackTrace();
					return;
				}
			}
	}

}
